package com.tinyseed.emcp.api.maintenance.state;

import com.tinyseed.emcp.api.exception.entity.ExceptionInfo;
import com.tinyseed.emcp.api.exception.service.ExceptionInfoService;
import com.tinyseed.emcp.api.exception.state.CloseState;
import com.tinyseed.emcp.api.exception.state.DeclaredUnprocessedState;
import com.tinyseed.emcp.api.maintenance.domain.MaintenanceInfo;
import com.tinyseed.emcp.api.maintenance.service.MaintenanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @version Revision 1.0.0
 * @版权：
 * @author: Howard
 * @email:
 * @see:
 * @创建日期：2020/3/14
 * @功能说明：维保创建状态
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2020/3/14 wuxiran        创建
 * @end
 */
@Service
@Slf4j
public class MTNewState extends MaintenanceState {

    public static final String NEW_STATE_NAME = "审核中";
    public static final String NEW_STATE_CODE = "NEW_STATE";

    @Autowired
    private MaintenanceService maintenanceService;

    @Autowired
    private ExceptionInfoService exceptionInfoService;

    @Override
    public void handleMaintenanceInfo(MaintenanceInfo context) {

    }

    @Override
    public void nextStep(MaintenanceInfo context) {
        /**
         * Description 变更状态为审核通过
         * Version 1.0 
         * Date 2020/3/15 5:36 下午
         * Author howard
         * @param context
         * @return void
         */
        context.setMtStatus(MTAuditedState.AUDITED_STATE_NAME);
        context.setMtStatusCode(MTAuditedState.AUDITED_STATE_CODE);
        maintenanceService.save(context);
    }

    public void reject(MaintenanceInfo context) {
        /**
         * Description reject拒绝
         * Version 1.0
         * Date 2020/3/15 9:16 下午
         * Author howard
         * @param context
         * @return void
         */
        context.setMtStatus(MTAuditedState.AUDITED_STATE_NAME);
        context.setMtStatusCode(MTAuditedState.AUDITED_STATE_CODE);
        maintenanceService.save(context);
        log.info("拒绝暂无");
    }

    public void close(MaintenanceInfo context) {
        /**
         * Description close关闭维保状态，并且关闭异常
         * Version 1.0 
         * Date 2020/3/15 9:22 下午 
         * Author howard
         * @param context
         * @return void
         */
        context.setMtStatusCode(MTClosedState.MT_CLOSE_STATE_CODE);
        context.setMtStatus(MTClosedState.MT_CLOSE_STATE_NAME);
        ExceptionInfo exceptionInfo = context.getExceptionInfo();

        exceptionInfo.setCurrentStatusCode(CloseState.CLOSE_STATE_CODE);
        exceptionInfo.setCurrentStatus(CloseState.CLOSE_STATE_NAME);

        maintenanceService.save(context);
    }

    public void submitExceptionAndMT(MaintenanceInfo maintenanceInfo) {
        /**
         * Description submitExceptionAndMT 申报异常
         * Version 1.0 
         * Date 2020/3/15 9:48 下午
         * Author howard
         * @param maintenanceInfo
         * @return void
         */
        ExceptionInfo exceptionInfo = maintenanceInfo.getExceptionInfo();
        exceptionInfo.setCurrentStatusCode(DeclaredUnprocessedState.DECLARED_UNPROCESSED_STATE_CODE);
        exceptionInfo.setCurrentStatus(DeclaredUnprocessedState.DECLARED_UNPROCESSED_STATE_NAME);

        maintenanceInfo.setMtStatusCode(MTDeclaredUnprocessedState.DECLARED_UNPROCESSED_STATE_CODE);
        maintenanceInfo.setMtStatus(MTDeclaredUnprocessedState.DECLARED_UNPROCESSED_STATE_NAME);

        exceptionInfoService.saveExceptionInfo(exceptionInfo);
        maintenanceService.save(maintenanceInfo);
    }

    @Override
    public void prevStep(MaintenanceInfo context) {
        log.info("这个是初始状态");
    }

}
